﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Library;

namespace OA.Exam
{
    public partial class QuestionsManage : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTypes();//绑定题型

                myExam.GetQuestionGroups(DdlGroup);//绑定分组

                BindQuestions();//绑定题目列表
            }
        }

        protected void BindTypes()
        {
            DdlType.DataSource = Exam.GetQuestionTypes();
            DdlType.DataBind();
        }

        protected void BindQuestions()
        {
            string sql = "select QuestionId,Subject,TypeName from Exam_Questions as a left join Exam_QuestionTypes as b on a.ItemType=b.TypeId where 1=1";
            if (DdlType.SelectedValue != "-1")
            {
                sql += " and ItemType='" + DdlType.SelectedValue + "'";
            }
            if (DdlGroup.SelectedValue != "-1")
            {
                sql += " and QuestionId in (select QuestionId from Exam_QuestionsGroups where GroupId ='" + DdlGroup.SelectedValue + "')";
            }
            if (TxtSubject.Text.Trim() != "")
            {
                sql += " and Subject like '%" + TxtSubject.Text.Trim() + "%'";
            }
            sql += " order by a.QuestionId desc";

            DataTable dt = DataAccess.getDataTable(sql);
            GridViewQuestions.DataSource = dt.DefaultView;
            GridViewQuestions.DataBind();
        }

        protected void GridViewQuestions_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewQuestions.PageIndex = e.NewPageIndex;
            BindQuestions();
        }
        //查询
        protected void BtnSearcn_Click(object sender, EventArgs e)
        {
            BindQuestions();
        }
        //新增
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuestionMaintain.aspx");
        }

        protected void GridViewQuestions_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //修改题目
            if (e.CommandName == "EditQuestion")
            {
                int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                string qId = GridViewQuestions.DataKeys[rowIndex].Value.ToString();
                HiddenQid.Value = qId;
                Response.Redirect("QuestionMaintain.aspx?qId=" + qId);
            }
            //删除题目
            if (e.CommandName == "DeleteQuestion")
            {
                int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                string qId = GridViewQuestions.DataKeys[rowIndex].Value.ToString();
                HiddenQid.Value = qId;
                //先判断题目是否包含于试卷中
                if (myExam.IsInPaper(qId))
                {
                    LabPrompt.Text = "题目包含于试卷中，暂时不能删除";
                    return;
                }
                else
                {
                    if (myExam.DeleteQuestion(qId))
                    {
                        LabPrompt.Text = "删除成功";
                        BindQuestions();//绑定题目列表
                    }
                    else
                    {
                        LabPrompt.Text = "删除失败";
                    }
                }
            }
            //设置分组
            if (e.CommandName == "SetQuestion")
            {
                int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                string qId = GridViewQuestions.DataKeys[rowIndex].Value.ToString();
                HiddenQid.Value = qId;
                //生成树形结构
                myExam.GetGroupTree(TreeView1);
                TreeView1.Attributes.Add("onclick", "javascript:return GoSelect();");
                //绑定已选分组
                myExam.BindHasCheckGroup(TreeView1, qId);
                ModalPopupExtender1.Show();
            }
       }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            int mark = 0;
            for (int i = 0; i < GridViewQuestions.Rows.Count; i++)
            {
                string qId = GridViewQuestions.DataKeys[i].Value.ToString();
                CheckBox ChbCheck = (CheckBox)GridViewQuestions.Rows[i].FindControl("ChbCheck");
                if (ChbCheck.Checked)
                {
                    mark++;
                    //先判断题目是否包含于试卷中
                    if (myExam.IsInPaper(qId))
                    {
                        LabPrompt.Text = "题目包含于试卷中，暂时不能删除";
                        return;
                    }
                    else
                    {
                        if (!myExam.DeleteQuestion(qId))
                        {
                            LabPrompt.Text = "删除失败";
                        }
                        else
                        {
                            LabPrompt.Text = "删除成功";
                        }
                    }
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "请选择题目";
                return;
            }
            BindQuestions();//绑定题目列表
        }

        protected void BtnOk_Click(object sender, EventArgs e)
        {
            string qId = HiddenQid.Value;
            bool flag = false;
            //先删除所有分组
            if (myExam.DeleteAllGroups(qId))
            {
                for (int i = 0; i <TreeView1.CheckedNodes.Count; i++)
                {
                    flag = myExam.InsertGroup(qId, TreeView1.CheckedNodes[i].Value.ToString());
                }
                if (flag)
                {
                    LabPrompt.Text = "选择分组成功";
                }
            }
        }
    }
}
